package com.fasteat.mall.controller;

import com.fasteat.common.entity.Result;
import com.fasteat.mall.service.VisitedHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ghost
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/mall/user-product/history")
@Api(description = "用户商品浏览记录模块")
@CrossOrigin
public class ProductVisitedHistoryController {

	@Autowired
	private VisitedHistoryService historyService;

	@GetMapping("/{userId}")
	@ApiOperation("查询用户所有浏览商品历史记录")
	public Result searchAllVisitedHistoriesOfProductForUser(@PathVariable(name = "userId")
															@ApiParam(value = "用户id", required = true) String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(historyService.getAllProductVisitedHistoriesByUserId(userId));
	}

	@GetMapping("/{userId}/{size}/{current}")
	@ApiOperation("分页查询查询用户浏览商品历史记录")
	public Result pageAllVisitedHistoriesOfProductForUser(@PathVariable(name = "userId")
														  @ApiParam(value = "用户id", required = true) String userId,
														  @PathVariable(name = "size")
														  @ApiParam(value = "尺寸", required = true) int size,
														  @PathVariable(name = "current")
														  @ApiParam(value = "页码", required = true) int current) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(
				historyService.getAllProductVisitedHistoriesByUserIdWithPage(userId, current, size));
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除某个浏览商品历史记录")
	public Result deleteVisitedHistoryOfProductForUser(@PathVariable(name = "id")
													   @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		return historyService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
	}

	@DeleteMapping("/all/{userId}")
	@ApiOperation("删除所有浏览商品历史记录")
	public Result deleteAllVisitedHistoriesOfProductForUser(@PathVariable(name = "userId")
															@ApiParam(value = "用户id", required = true) String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		return historyService.removeAllAllProductVisitedHistoriesByUserId(userId) ? Result.success("删除成功") : Result.error("删除失败");
	}

}
